<?php

class Admin extends AppModel
{

	public $name = 'Admin';

	public $displayField = 'username';

	public $statuses = array(
        'ACTIVE',
        'INACTIVE',
	);

	public $validate = array(
        'username' => array(
            'create_first' => array(
                'on' => 'create',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'required',
            ),
            'update_first' => array(
                'on' => 'update',
                'required' => false,
                'allowEmpty' => false,
                'message' => 'required',
            ),
            'second_1' => array(
                'rule' => array('lengthBetween', 3, 15),
                'message' => 'valid_length_rule_username',
            ),
            'second_2' => array(
                'rule' => array('customEreg', '^[0-9a-zA-Z_]([:.-]?([0-9a-zA-Z_]))*$'),
                'message' => 'valid_text_rule_username',
            ),
            'third' => array(
                'rule' => array('allowUserName'),
                'message' => 'valid_login_exist',
            ),
        ),
		'password' => array(
            'create_first' => array(
                'on' => 'create',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'required',
            ),
            'update_first' => array(
                'on' => 'update',
                'required' => false,
                'allowEmpty' => false,
                'message' => 'required',
            ),
            'first' => array(
                'rule' => array('lengthBetween', 6, 20),
                'message' => 'valid_text_rule_password',
            ),
            'second_1' => array(
             	'rule' => array('customEreg', '[[:upper:]]'),
                'message' => 'valid_pass_alfa_dig',
            ),
            'second_2' => array(
             	'rule' => array('customEreg', '[[:lower:]]'),
                'message' => 'valid_pass_alfa_dig',
            ),
            'second_3' => array(
             	'rule' => array('customEreg', '[[:digit:]]'),
                'message' => 'valid_pass_alfa_dig',
            ),
        ),
		'confirm_password' => array(
            'create_first' => array(
                'on' => 'create',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'valid_cpassword_default',
            ),
            'update_first' => array(
                'on' => 'update',
                'required' => false,
                'allowEmpty' => false,
                'message' => 'valid_cpassword_default',
            ),
            'first' => array(
                'rule' => array('equalWith', 'password'),
                'message' => 'valid_cpassword_default',
            ),
        ),
	);

    function allowUserName($data)
    {
        $otherModel =& $this->getModel('ClientAdmin');
        $otherModel->rel();
        $this->rel();
		return ! is_array($otherModel->find(array("`username`" => $data)))
		&&
        ! is_array($this->find(array("`{$this->primaryKey}`" => "!=".intval($this->id), "`username`" => $data)));
    }

    function createDefaultAdmin()
    {
        $existing = (bool)$this->findCount();
        if (!empty($existing)) {
            throw new Exception();
        }
        $data = array();
        $data[$this->name]['username'] = Application::config()->getDefaultAdminLogin();
        $data[$this->name]['password'] = Application::config()->getDefaultAdminPassword();
        $data[$this->name]['status'] = 'ACTIVE';
        return $this->save($this->create($data), false);
    }

	function beforeValidate()
	{
	    if (isset($this->data[$this->name]) && is_array($this->data[$this->name])) {
    	    if (array_key_exists('password', $this->data[$this->name])) {
        	    $pass_length = mb_strlen($this->data[$this->name]['password']);
        	    if (!$pass_length || $pass_length==32) {
        	        unset($this->data[$this->name]['password']);
        	    }
            }
            if (!array_key_exists('password', $this->data[$this->name]) && array_key_exists('confirm_password', $this->data[$this->name])) {
    	        unset($this->data[$this->name]['confirm_password']);
            }
	    }
	    return true;
	}

	function beforeSave()
	{
	    $creating = ! $this->exists();
	    if ($creating) {
            $this->data[$this->name]['created'] = date('Y-m-d H:i:s');
	    }
        if (array_key_exists('password', $this->data[$this->name])) {
            $this->data[$this->name]['password'] = md5($this->data[$this->name]['password']);
        }
	    return parent::beforeSave();
	}

	function getStatuses()
	{
	    return $this->statuses;
	}

	function getStatusesList()
	{
	    $result = array();
	    foreach ($this->statuses as $type) {
	        $result[$type] = __d('admins', 'status_'.$type, true);
	    }
	    return $result;
	}

	function allowDelete($fields = null)
	{
        return false;
	}

	function allowUpdate($fields = null)
	{
        return true;
	}

}

?>